<?php 
/* 2. Domain Tagging, Jr.
----------------------
Create an application using PHP and MySQL that hundreds of thousands of
Internet users will use to track Web site domain names and accompanying
descriptions.

Your application should provide two Web pages:
1. One that allows users to submit one domain and description at a
time.
2. One that displays all valid domains along with their descriptions
and the submission date, ordered from newest to oldest.

For our purposes, a valid domain is one that has one or more IP
addresses in the DNS.

Your applications should include a table called "domain" that stores:
1. the domain name, whether or not it is valid
2. the description
3. the date and time of submission.

Keep in mind that users might attempt to submit any sort of text-like
data as a domain, e.g., "http://www.opendns.com/about/",
"notareal.comdomain", or pretty much anything else.

Also, domains should be stored in the database without any "www" prefix
and without any other URL components. For example,
"http://www.opendns.com/about/" should be stored as "opendns.com".
*/
    include 'install.php';
    
    function addDomain($url, $description, $host, $dbname, $user, $pass)
    {
        if (filter_var($url, FILTER_VALIDATE_URL) !== false)
        {
            try {
                $db = new PDO("mysql:host=$host;dbname=$dbname;", $user, $pass);
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              
                $sql = "INSERT INTO domains (domainName, description, dns_timestamp) VALUES (:domain, :domaindescription, NOW())";
                $preparedStatement = $db->prepare($sql);
                $preparedStatement->bindParam(':domain', $domain);
                $preparedStatement->bindParam(':domaindescription', $domaindescription);
                
                $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
                $domaindescription = filter_var($description, FILTER_SANITIZE_STRIPPED);
                $preparedStatement->execute();                
            } catch (PDOException $e) {
                trigger_error("DB ERROR: ". $e->getMessage());
            }
        } else {
            echo 'Danger, Will Robinson! Danger! <br/>There was an issue with your submission.  Please check to make sure you entered a valid URL: <br/>'.$url;
        }
    }
    
    function getDomains($host, $dbname, $user, $pass)
    {
      try {
            $db = new PDO("mysql:host=$host;dbname=$dbname;", $user, $pass);
            $sql = 'SELECT SQL_NO_CACHE domainName, description, dns_timestamp FROM domains ORDER BY dns_timestamp DESC';
            $preparedstatement = $db->prepare($sql);
            foreach($db->query($sql) as $row) 
            {
                echo '<tr><td>'.$row['domainName'].'</td><td>'.$row['description'].'</td><td>'.$row['dns_timestamp'].'</td></tr>';
            }
        } catch (PDOException $e) {
            trigger_error('DB ERROR: ' . $e->getMessage());
        }
    }

?>
<html>
    <head>
        <title>View DNS Domains</title>
        <link href="styles.css" rel="stylesheet">
    </head>
    <body>
        <h1>View DNS Domains</h1>
        
        <?php 
        //add check to see if URL and DESCRIPTION isset and not empty.
        $isvalDomain = isset($_POST["domainName"]) && !empty($_POST["domainName"]);  
        $isvalDescription = isset($_POST["domainDescription"]) && !empty($_POST["domainDescription"]); 
        if ($isvalDomain && $isvalDescription)
        {
            addDomain($_POST["domainName"], $_POST["domainDescription"], $host, $dbname, $user, $pass);
            header('Location: domain-tagging.php');
        }
        ?>
        <div class="container">
            <p>If you don't see a valid domain name here, <a href="submitDomain.php">add it to our database</a>!</p>
            <table border="1">
                <tr>
                    <th>Domain</th>
                    <th>Description</th>
                    <th>Submission Date</th>
                </tr>
                <?php
                    getDomains($host, $dbname, $user, $pass);
                ?>
            </table>
        </div>
    </body>
</html>